<?php
namespace app\weixin\controller;

use think\Controller;
use \app\common\model\Config;
use \app\common\model\WXUser;
use \app\common\model\TmpMobile;
use \app\common\libs\WeixinHelper;
use \app\common\libs\WeixinTpl;
use \app\common\libs\Tuling;
use \CURLFile;

class Index extends Controller
{
	use \app\weixin\traits\Weixin;
	public function index(){

		// 如果是GET请求，代表是微信验证请求
		if(request()->isGet()){
			$this->wxVerification();
			die ('success');
		}

		$wx_data = \get_wxd();
		$config_model = new Config;
		$token = $config_model->getToken();

		trace(json_encode($wx_data), 'weixin');

		if($wx_data['MsgType'] === 'event') {
			switch ($wx_data['Event']) {
				case 'subscribe':
					$this->addSubscribe($wx_data);
					// 发送欢迎消息
					$welcome_text = $config_model->getWelcomeText();
					die(WeixinTpl::text($wx_data['ToUserName'], $wx_data['FromUserName'], $welcome_text));
					break;

				case 'unsubscribe'://取消关注事件
					$weixin_user_model = new WXUser;
					$weixin_user_model->unsubscribe($wx_data['FromUserName'], $wx_data['ToUserName']);
					break;
				case 'CLICK'://菜单事件, 对应 CLICK 类型菜单,见《自定义菜单》章节   
					// 菜单点击事件
					$this->wxClickEvent($wx_data);
					break;
			}
		} else {
			if($wx_data['MsgType'] === 'text') {
				$text = trim($wx_data['Content']);
				if(preg_match(config('reg.mobile'), $text)) {
					$this->wxRegisterMobile($wx_data);
				}
				if(preg_match(config('reg.vcode'), $text)) {
					$this->wxRegisterVcode($wx_data);
				}
			}
			
			//被动回复消息
            $this->tulingReply($wx_data);
		}

		die ('success');
	}
}

